//
//  UIViewController+Extension.swift
//  SDKDebuggingDemo
//
//  Created by 秦晟哲 on 2019/8/22.
//  Copyright © 2019 QinszQSZ. All rights reserved.
//

import Foundation

typealias DateInputCompletion = (KCTBLEDate) -> Void

typealias TextInputCompletion = (_ text:String) -> Void

typealias MultiTextInputCompletion = (_ textArray:[String]) -> Void

extension UIViewController {
    
    
    /// 单行输入框
    func showTextInput(title:String?,message:String?,completion:@escaping TextInputCompletion){
        let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        alert.addTextField { (textField) in
            
        }
        let confirm = UIAlertAction.init(title: NSLocalizedString("确定", comment: ""), style: .default) { (action) in
            completion(alert.textFields?.first?.text ?? "")
        }
        let cancel = UIAlertAction.init(title: NSLocalizedString("取消", comment: ""), style: .cancel) {(action) in
        }
        alert.addAction(cancel)
        alert.addAction(confirm)
        self.present(alert, animated: true, completion: nil)
    }
    
    
    /// 日期输入框
    func showDateInput(title:String?,message:String?,completion:@escaping DateInputCompletion) {
        
        let now = Date.init()
        let calendar = Calendar.current
        let year = calendar.component(.year, from: now)
        let month = calendar.component(.month, from: now)
        let day = calendar.component(.day, from: now)
        let hour = calendar.component(.hour, from: now)
        let minute = calendar.component(.minute, from: now)
        let second = calendar.component(.second, from: now)
        
        
        let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        alert.addTextField { (textField) in
            textField.keyboardType = .numberPad
            textField.placeholder = "年"
            textField.text = "\(year)"
        }
        alert.addTextField { (textField) in
            textField.keyboardType = .numberPad
            textField.placeholder = "月"
            textField.text = "\(month)"
        }
        alert.addTextField { (textField) in
            textField.keyboardType = .numberPad
            textField.placeholder = "日"
            textField.text = "\(day)"
        }
        alert.addTextField { (textField) in
            textField.keyboardType = .numberPad
            textField.placeholder = "时"
            textField.text = "\(hour)"
        }
        alert.addTextField { (textField) in
            textField.keyboardType = .numberPad
            textField.placeholder = "分"
            textField.text = "\(minute)"
        }
        alert.addTextField { (textField) in
            textField.keyboardType = .numberPad
            textField.placeholder = "秒"
            textField.text = "\(second)"
        }
        let confirm = UIAlertAction.init(title: NSLocalizedString("确定", comment: ""), style: .default) {[weak alert] (action) in
            
            let date = KCTBLEDate.init()
            
            if let fields = alert?.textFields {
                for i in 0 ..< fields.count {
                    let text = fields[i].text ?? ""
                    if let value = Int(text) {
                        switch i {
                        case 0:
                            date.year = value
                        case 1:
                            date.month = value
                        case 2:
                            date.day = value
                        case 3:
                            date.hour = value
                        case 4:
                            date.minute = value
                        case 5:
                            date.second = value
                        default:
                            IPrint("default")
                        }
                    }
                }
            }
            
            completion(date)
        }
        let cancel = UIAlertAction.init(title: NSLocalizedString("取消", comment: ""), style: .cancel) {(action) in
        }
        alert.addAction(confirm)
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)
        
    }
    
    func showMultiTextInput(title:String?,message:String?,lines:Int,placeHolders:[String]?,completion:@escaping MultiTextInputCompletion){
        let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        for i in 0..<lines {
            var placeHolder:String?
            if let ps = placeHolders,i < ps.count {
                placeHolder = ps[i]
            }
            alert.addTextField { (field) in
                field.placeholder = placeHolder
            }
        }
        let confirm = UIAlertAction.init(title: NSLocalizedString("确定", comment: ""), style: .default) { (action) in
            var textArray = Array<String>.init()
            if let fields = alert.textFields {
                for i in 0 ..< fields.count {
                    textArray.append(fields[i].text ?? "")
                }
            }
            completion(textArray)
        }
        let cancel = UIAlertAction.init(title: NSLocalizedString("取消", comment: ""), style: .cancel) {(action) in
        }
        alert.addAction(cancel)
        alert.addAction(confirm)
        self.present(alert, animated: true, completion: nil)
    }
    
    func showMultiTextInput(title:String?,message:String?,lines:Int,placeHolders:[String]?,contents:[String]?,completion:@escaping MultiTextInputCompletion){
        let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        for i in 0..<lines {
            
            var placeHolder:String?
            if let ps = placeHolders,i < ps.count {
                placeHolder = ps[i]
            }
            
            var content : String?
            if let cs = contents,i < cs.count {
                content = cs[i]
            }
            
            alert.addTextField { (field) in
                field.placeholder = placeHolder
                field.text = content
            }
        }
        let confirm = UIAlertAction.init(title: NSLocalizedString("确定", comment: ""), style: .default) { (action) in
            var textArray = Array<String>.init()
            if let fields = alert.textFields {
                for i in 0 ..< fields.count {
                    textArray.append(fields[i].text ?? "")
                }
            }
            completion(textArray)
        }
        let cancel = UIAlertAction.init(title: NSLocalizedString("取消", comment: ""), style: .cancel) {(action) in
        }
        alert.addAction(cancel)
        alert.addAction(confirm)
        self.present(alert, animated: true, completion: nil)
    }

    
}
